package com.huiminpay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.StringUtil;
import com.huiminpay.merchant.convert.MerchantConvert;
import com.huiminpay.merchant.entity.Merchant;
import com.huiminpay.merchant.entity.Staff;
import com.huiminpay.merchant.entity.Store;
import com.huiminpay.merchant.entity.StoreStaff;
import com.huiminpay.merchant.mapper.MerchantMapper;
import com.huiminpay.merchant.mapper.StaffMapper;
import com.huiminpay.merchant.mapper.StoreMapper;
import com.huiminpay.merchant.mapper.StoreStaffMapper;
import com.huiminpay.user.api.TenantService;
import com.huiminpay.user.api.dto.tenant.CreateTenantRequestDTO;
import com.huiminpay.user.api.dto.tenant.TenantDTO;
import com.yh.merchant.api.IMerchantService;
import com.yh.merchant.api.dto.MerchantDTO;
import com.yh.merchant.api.dto.StoreStaffDTO;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

@org.apache.dubbo.config.annotation.Service
public class MerchantServiceImpl implements IMerchantService {
    @Autowired
    MerchantMapper merchantMapper;
    @Autowired
    StoreMapper storeMapper;
    @Autowired
    StaffMapper staffMapper;
    @Autowired
    StoreStaffMapper storeStaffMapper;

    @Reference
    TenantService tenantService;
    @Override
    public MerchantDTO queryMerchantById(Long merchantId) {
        //返回的是entity类型
        Merchant merchant = merchantMapper.selectById(merchantId);
//        MerchantDTO merchantDTO = new MerchantDTO();
//        BeanUtils.copyProperties(merchant,merchantDTO);
        return MerchantConvert.INSTANCE.entity2dto(merchant);
    }

    @Override
    public MerchantDTO reg(MerchantDTO merchantDTO)throws BusinessException {
        //非空校验
        if(StringUtil.isEmpty(merchantDTO.getMobile())){
            throw  new BusinessException(CommonErrorCode.E_200230);
        }
        if(StringUtil.isEmpty(merchantDTO.getUsername())){
            throw  new BusinessException(CommonErrorCode.E_200231);
        }
        if(StringUtil.isEmpty(merchantDTO.getPassword())){
            throw  new BusinessException(CommonErrorCode.E_200232);
        }
        //1.手机号唯一校验(查询数据库)
        QueryWrapper<Merchant> qw = new QueryWrapper<>();
        qw.lambda().eq(Merchant::getMobile,merchantDTO.getMobile());
        Integer integer = merchantMapper.selectCount(qw);
        if (integer>0){
            throw  new BusinessException(CommonErrorCode.E_200203);
        }
        //3.新增租户并初始化管理员
        CreateTenantRequestDTO createTenantRequestDTO =  new CreateTenantRequestDTO();
        createTenantRequestDTO.setName(merchantDTO.getUsername());
        createTenantRequestDTO.setBundleCode("huimin-merchant");
        createTenantRequestDTO.setTenantTypeCode("huimin-merchant");
        createTenantRequestDTO.setUsername(merchantDTO.getUsername());
        createTenantRequestDTO.setPassword(merchantDTO.getPassword());
        createTenantRequestDTO.setMobile(merchantDTO.getMobile());
        TenantDTO tenantAndAccount = tenantService.createTenantAndAccount(createTenantRequestDTO);
//        Merchant merchant = new Merchant();
//        BeanUtils.copyProperties(merchantDTO,merchant);
        Merchant merchant = MerchantConvert.INSTANCE.dto2entity(merchantDTO);
        //业务字段更新
        merchant.setAuditStatus("0");
        //TODO  tenantId
        merchant.setTenantId(tenantAndAccount.getId());
        //保存数据
        merchantMapper.insert(merchant);
//        merchantDTO.setId(merchant.getId());

        //初始化门店信息（新增门店，新增员工，员工绑定门店（设置管理员））
        Store store = new Store();
        store.setStoreName("根门店");
        store.setStoreNumber(10000L);
        store.setMerchantId(merchant.getId());
        store.setParentId(0L);
        store.setStoreStatus(true);
        storeMapper.insert(store);
        //初始化员工信息（管理员）
        Staff staff = new Staff();
        staff.setStoreId(store.getId());
        staff.setMobile(merchantDTO.getMobile());
        staff.setUsername(merchantDTO.getUsername());
        staff.setMerchantId(merchant.getId());
        staff.setStaffStatus(true);
        staffMapper.insert(staff);

        //给门店绑定管理员
        StoreStaff storeStaff = new StoreStaff();
        storeStaff.setStaffId(staff.getId());
        storeStaff.setStoreId(store.getId());
        storeStaffMapper.insert(storeStaff);
        return MerchantConvert.INSTANCE.entity2dto(merchant);
    }

    @Override
    public MerchantDTO applay(MerchantDTO merchantDTO) throws BusinessException{
        //对象转换
        Merchant merchant = MerchantConvert.INSTANCE.dto2entity(merchantDTO);
        //业务字段屏蔽  有些字段不能更新 设置为null 即不更新
        merchant.setMobile(null);
        merchant.setUsername(null);
        merchant.setTenantId(null);
        merchant.setAuditStatus("1");
        //更新数据
        merchantMapper.updateById(merchant);
        return MerchantConvert.INSTANCE.entity2dto(merchant);
    }

    @Override
    public MerchantDTO queryMerchantByTenantId(Long tenantId) {
        QueryWrapper<Merchant> qw = new QueryWrapper<>();
        qw.lambda().eq(Merchant::getTenantId,tenantId);
        Merchant merchant = merchantMapper.selectOne(qw);
        return MerchantConvert.INSTANCE.entity2dto(merchant);
    }
}
